//
//  WebViewController.m
//  quickLook
//
//  Created by J on 15/9/1.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "WebViewController.h"
#import "Header.h"

#import "CommentController.h"

#import "ThirdNaviSearchController.h"

#import "LandController.h"
@interface WebViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate,UMSocialUIDelegate>
{
    BOOL _isSelected;
    BOOL _isExisted;


}
@property (weak, nonatomic) IBOutlet UIView *customView;

@property(nonatomic,strong)IBOutlet UIWebView* webView;

@property(nonatomic,strong)UIToolbar * toolBar;

@property (weak, nonatomic) IBOutlet UIView *separateView;



//自定义toolbar 成员变量  用来tap隐藏设置

@property (weak, nonatomic) IBOutlet UIImageView *thumb;
@property (weak, nonatomic) IBOutlet UIImageView *comment;

@property (weak, nonatomic) IBOutlet UIImageView *share;


//评论个数

@property (weak, nonatomic) IBOutlet UILabel *commentCountNumber;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
    
    
    if (_model.value) {
        [self loadData];

    }
  
}



- (void)loadData{
    
    
    
    
    
    [DownLoad getADScrollViewSkipDataWithValue:_model.value   andBlock:^(NSDictionary* obj, NSError *error) {
        
        
        
        Model* model=[[Model alloc]init];
        
        _model.url=obj[@"data"][@"url"];
        
        [model setValuesForKeysWithDictionary:obj[@"data"]];
        
        _model=model;
        
        NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:_model.url]];
        
        [_webView loadRequest:request];
        
        _commentCountNumber.text=[NSString stringWithFormat:@"%@",_model.comments_count];

    
    }];






}

- (void)viewWillAppear:(BOOL)animated

{


    [super viewWillAppear:YES];
    _commentCountNumber.text=[NSString stringWithFormat:@"%@",_model.comments_count];
    [self isExist];
    
    
    
    

}

- (void)createUI
{
    
    
    //如果没网 直接停止动画
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    BOOL isConnection = NO;
    
    isConnection=[userdefault boolForKey:@"isConnection"];
    
    if (!isConnection) {

        [ProgressHUD showError:@"无网络连接" Interaction:YES];
    }

    
    
    

    _commentCountNumber.layer.cornerRadius=7;
    _commentCountNumber.clipsToBounds=YES;

    
    
    
    self.automaticallyAdjustsScrollViewInsets=NO;
   
   self.view.backgroundColor=[UIColor whiteColor];
//    _webView=[[UIWebView alloc]initWithFrame:self.view.bounds];
    
    _webView.delegate=self;

//    [self.view addSubview:_webView];

    
    NSURLRequest* request=[NSURLRequest requestWithURL:[NSURL URLWithString:_model.url]];
    
    [_webView loadRequest:request];

    self.navigationItem.title=_model.title;
    UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
    

    
    
    [_webView addGestureRecognizer:tap];
    
    self.view.userInteractionEnabled=YES;
    _webView.userInteractionEnabled=YES;
    
    tap.delegate=self;
    
    
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"全集" style:UIBarButtonItemStyleDone target:self action:@selector(rightPush:)];
    
    

    //左侧导航跳转按钮
    
    
    UIBarButtonItem* leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"naviBackButton.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(pop:)];
    
    
    self.navigationItem.leftBarButtonItem=leftBarButtonItem;
    

    
    
    
    
    
    
    
    
    [self isExist];
    
    

}
- (void)isExist{


    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];
    
    
    [mutableArray enumerateObjectsUsingBlock:^(NSNumber* obj, NSUInteger idx, BOOL *stop) {
        
        if ([_model.topic[@"id"] isEqualToNumber:obj]) {
            
            //            model.likes_count=[NSNumber numberWithInteger:[model.likes_count integerValue]+1];
            _thumb.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];
            _isSelected=YES;
            _isExisted=YES;
            
        }else{
        
            _thumb.image=[UIImage imageNamed:@"quickLook2.png"];
            _isSelected=NO;
            _isExisted=NO;
        }
        
    }];
    
  
}

- (void)pop:(id)sender {
    
    
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//右侧 全集 barbutton跳转
- (void)rightPush:(UIBarButtonItem*)sender
{

    ThirdNaviSearchController* vc=[[ThirdNaviSearchController alloc]init];
    
    
    Model* model=[[Model alloc]init];
    if (_model.topic_id) {
        
        
        model.id=_model.topic_id;

        
    }else
    {
    
    model.id=_model.topic[@"id"];
        
        
    }
    
    vc.model=model;//千万别写错了 用新创建的model
    
    vc.isScreen=YES;
    
    [self.navigationController pushViewController:vc animated:YES];
 
    
    
    
}



//单击隐藏navigationbar
    
- (void)tap:(UITapGestureRecognizer*)tap
{

    
    
    if ((tap.view.tag=!tap.view.tag)) {
        
        
        
        self.navigationController.navigationBar.hidden=YES;
        _webView.scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT);

    
        _commentCountNumber.alpha=0;
        
        _customView.alpha=0;
        
        _share.alpha=0;
        _comment.alpha=0;
        _thumb.alpha=0;

        
        _separateView.alpha=0;
        
        
    }else
    {
        
        _webView.scrollView.frame=CGRectMake(0, 64, SCREEN_WIDTH,SCREEN_HEIGHT);

        self.navigationController.navigationBar.hidden=NO;
    
        
//        _customView.backgroundColor=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];

        _customView.alpha=1;
        
        _commentCountNumber.alpha=1;
        _share.alpha=1;
        _comment.alpha=1;
        _thumb.alpha=1;
        _share.userInteractionEnabled=YES;
        _separateView.alpha=1;


    }
    
    
    

}


//点击放大
- (IBAction)scale:(UITapGestureRecognizer *)sender {

    
    //如果没有登录 无法点击放大效果 跳转登录界面
    //无网络连接 无法点击
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    BOOL isConnection = NO;
    
    isConnection=[userdefault boolForKey:@"isConnection"];
    
    if (!isConnection) {
        return;
    }
    
    
    BOOL isLand=[userdefault boolForKey:ISLAND];//BOOL
    
    if (!isLand) {
        LandController* vc=[[LandController alloc]init];
        
        [self presentViewController:vc animated:YES completion:nil];
        
        
        //        [ProgressHUD showError:@"未登录" Interaction:YES];
        
        return;
    }

    

    
    isConnection=[userdefault boolForKey:@"isConnection"];
    
    if (!isConnection) {
        return;
    }
    

    
//    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];

    
    
    
    
    [UIView animateWithDuration:0.2 animations:^{
        
        _thumb.transform= CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0);
        
        if (_isSelected==NO) {
            
            
            
        
            
            _thumb.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];
            
            
            
            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            
            [mutaArray addObjectsFromArray:mutableArray];
            
            if (_isExisted==NO) {
                [mutaArray addObject:_model.topic[@"id"]];
                [userdefault setObject:mutaArray forKey:IDArray];
                [userdefault synchronize];
                _isExisted=YES;

            }
            

            _isSelected=YES;
        }else{
            
            
            _isSelected=NO;

            _isExisted=NO;

            _thumb.image=[UIImage imageNamed:@"quickLook2.png"];
            
            
            
            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            [mutaArray addObjectsFromArray:mutableArray];
            [mutaArray removeObject:_model.topic[@"id"]];
            
            [userdefault setObject:mutaArray forKey:IDArray];
            
            
            [userdefault synchronize];

            
        }
        
//        JZJLog(@"xxxx%@",mutableArray);
        

        
        
              [DownLoad  getWetherDataWithID:_model.topic[@"id"] andBlock:^(NSDictionary* obj, NSError *error) {
            
            {
                
//                JZJLog(@"%@",obj);
                NSString* str=obj[@"message"];
                
                
                if ([str isEqual:@"OK"]) {
                    
                    
                    
                }
                
                
                
                
                
                
            }
        }];
        
        


        

        
        
        
    } completion:^(BOOL finished) {
        
        
        [UIView animateWithDuration:0.2 animations:^{
            _thumb.transform= CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
            
        }];
        
        
    }];
    
    
    
    
    JZJLog(@"%@",[userdefault objectForKey:IDArray]);


    
}


//点击跳转 评论页
- (IBAction)gotoCommentVC:(UITapGestureRecognizer *)sender {
    
    
    
    
    CommentController*vc= [CommentController new];
    
    
    vc.model=_model;
    
    
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
    
    
    
    

}


//友盟分享

- (IBAction)UMShare:(id)sender {
    
    
    
      NSString* shareStr=[NSString stringWithFormat:@"最近在极致漫画上看到一部超好看的漫画:%@ ,和我一起来看吧~ 完整内容戳: %@",_model.title,_model.url];
    
    UIImage*shareIamge= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.cover_image_url]]];
    //友盟分享
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5606db0ce0f55a58d1003a21"
                                      shareText:shareStr
                                     shareImage:shareIamge
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToSms,UMShareToRenren,nil]
                                       delegate:self];
    

    
}



//代理方法加载完成之后调用
- (void)webViewDidFinishLoad:(UIWebView *)webView
{


    NSString* abc =@"document.getElementsByClassName('app-fixed-banner')[0].remove();";

    
    
    [webView stringByEvaluatingJavaScriptFromString:abc];
    
    NSString * abcd = @"document.getElementsByClassName('app-banner cf')[0].remove();";
    
    [webView stringByEvaluatingJavaScriptFromString:abcd];
    
    



}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{

    return YES;

}






- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   
    
    
    if (navigationType==UIWebViewNavigationTypeLinkClicked) {
        return NO;

    }
    
    return YES;

}






@end
